module view {
    //地图
    export class MapView extends eui.Component {
        closeBtn: eui.Button
        mapId: number
        layerIndex: number
        mapNameLabel: eui.Label
        luckyLabel: eui.Label
        hpLabel: eui.Label
        bagBtn: eui.Button
        progressBar: eui.ProgressBar
        dataGroup: eui.Group
        progressIndex: number //地区 0探索第一个区域1第二个区域
        layerMap: number[]
        layerMapIndex: number
        adventureTimer: egret.Timer
        dataScroller: eui.Scroller
        advInfoLabel: eui.Label
        layerConfig: any
        mapLayerIndexLabel: eui.Label
        adventureInfoView: AdventureInfoView
        eventInfo: any
        public constructor(mapInfo: any) {
            super()
            this.mapId = mapInfo.mapId
            this.layerIndex = parseInt(mapInfo.layerIndex)
            this.progressIndex = 0
            var mapConfig = totalConfig[this.mapId]
            this.layerConfig = mapConfig.mapLayer[this.layerIndex]
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/MapSkin.exml"
            this.name = 'MapView'
        }

        private onComplete(): void {
            var adventureInfoView = new AdventureInfoView(this)
            adventureInfoView.y = 710
            this.addChild(adventureInfoView)
            this.adventureInfoView = adventureInfoView
            this.loadMapInfo()
            this.initProgress()
            var basicInfoView = new BasicInfoView()
            this.addChild(basicInfoView)
        }

        private bag(evt: egret.TouchEvent): void {
            var child = new view.BagView()
            this.stage.addChild(child)
        }

        private close(): void {
            this.parent.removeChild(this)
        }

        private loadMapInfo() {
            var mapConfig = totalConfig[this.mapId]
            this.mapNameLabel.text = mapConfig.name
            this.mapLayerIndexLabel.text = (this.layerIndex + 1) + '层'
        }

        private initProgress() {
            this.adventureTimer = new egret.Timer(500,0)
            this.adventureTimer['flag'] = true
            this.advInfoLabel.text = '区域一'
            this.adventureTimer.addEventListener(egret.TimerEvent.TIMER,this.startAdventure,this)
            this.adventureTimer.start()
        }
        //开始冒险
        private startAdventure(event: egret.TimerEvent) {
            if(this.adventureTimer['flag'] === true) {
                this.advInfoLabel.text = '正在探索中 ......'
                this.adventureTimer['flag'] = false
                return
            } else {
                this.adventureTimer['flag'] = true
            }
            if(!this.layerMap) {
                this.layerMap = this.layerConfig[this.progressIndex]
                if(!this.layerMap) {
                    this.adventureTimer.stop()
                }
                this.progressBar.maximum = this.layerMap.length
                this.progressBar.value = 0
                this.layerMapIndex = 0
            }
            var latticeId = this.layerMap[this.layerMapIndex]
            var latticeConfig = totalConfig[latticeId]
            var dataView = new AdventureData()

            this.dataGroup.addChild(dataView)
            if(this.dataScroller.viewport.contentHeight > this.dataScroller.height) {
                this.dataScroller.viewport.scrollV = this.dataScroller.viewport.contentHeight - this.dataScroller.height
            }
            this.layerMapIndex += 1
            this.progressBar.value += 1
            game.playerHandle.timeRun()
            if(this.layerMapIndex >= this.layerMap.length) {
                this.progressIndex += 1
                this.layerMap = null
                if(this.progressIndex >= this.layerConfig.length) {
                    this.advInfoLabel.text = '探索结束'
                    this.adventureTimer.stop()
                    return
                }
                this.advInfoLabel.text = '正在前往下一个区域中'
            } else {
                this.advInfoLabel.text = '正在进行的事情 :' + latticeConfig.name
                var info = game.adventureHandle.handle(latticeId,this)
                this.eventInfo = info
                if(info.type === 0) { //普通格子
                    dataView.setText(info.content)
                } else if(info.type === 1) { //事件
                    this.adventureTimer.stop()
                    this.popEventLayer(info.eventId)
                } else if(info.type === 2) {  //任务
                    var mapEventPopLayer = new MapTaskPopLayer1(info)
                    this.addChild(mapEventPopLayer)
                    this.adventureTimer.stop()
                }
            }
        }
        //战斗结束
        public battleEnd(){
            if(this.eventInfo.type === 2){ //任务
                var taskConfig = totalConfig[this.eventInfo.taskId]
                var latticeInfo:any
                for(var i in taskConfig.taskContent) {
                    if(taskConfig.taskContent[i].latticeId === this.eventInfo.latticeId) {
                        latticeInfo = taskConfig.taskContent[i]
                        break
                    }
                }
                if(latticeInfo.item){
                    var layer = new MapTaskPopLayer3(this.eventInfo)
                    this.addChild(layer)
                }else{
                    var mapEventPopLayer = new MapTaskPopLayer2(this.eventInfo)
                    this.addChild(mapEventPopLayer)
                }
                
            }else{
                this.adventureTimer.start()
            }          
        }
        
        public popLayer(latticeInfo) {
            var mapPopLayer = new MapPopLayer(latticeInfo)
            this.addChild(mapPopLayer)
        }

        public popEventLayer(latticeInfo) {
            var mapEventPopLayer = new MapEventPopLayer(latticeInfo)
            this.addChild(mapEventPopLayer)
        }

        public closePopLayer() {
            this.removeChildByName("MapPopLayer")
        }
    }

    class AdventureData extends eui.Component {
        label: eui.Label
        public constructor() {
            super()
            this.minWidth = 540
            this.minHeight = 10
            this.label = new eui.Label()
            this.addChild(this.label)
            this.label.width = 540
            this.label.minHeight = 10
            this.label.size = 24
            this.label.fontFamily = "楷体"
        }

        public setText(str) {
            this.label.text = str
        }
    }

    //任务面板1
    class MapTaskPopLayer1 extends eui.Component {
        confirmBtn: eui.Button
        cancelBtn: eui.Button
        contentLabel: eui.Label
        taskId: number
        taskInfo: any
        public constructor(taskInfo) {
            super()
            this.taskId = taskInfo.taskId
            this.skinName = "resource/control/MapPopSkin.exml"
            this.name = "MapPopLayer"
            this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this)
            this.cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.taskInfo = taskInfo
            this.contentLabel.text = this.taskInfo.latticeInfo.triggerContent
        }

        private touch(evt: egret.TouchEvent) {    
            var taskConfig = totalConfig[this.taskId] 
            var monsterId = taskConfig.monsterId
            var battleView = new view.BattleView2(monsterId)
            this.parent.addChild(battleView)
            this.parent.removeChild(this)          
        }

        private close(evt: egret.TouchEvent) {
            var child = this.stage.getChildByName('MapView')
            child['adventureTimer'].start()
            this.parent.removeChild(this)
        }
    }
    //任务面板2
    class MapTaskPopLayer2 extends eui.Component {
        confirmBtn: eui.Button
        cancelBtn: eui.Button
        contentLabel: eui.Label
        taskId: number
        taskInfo:any
        public constructor(taskInfo) {
            super()
            this.taskId = taskInfo.taskId
            this.skinName = "resource/control/MapPopSkin.exml"
            this.name = "MapPopLayer"
            this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this)
            this.cancelBtn.visible = false
            this.taskInfo = taskInfo
            this.contentLabel.text = this.taskInfo.latticeInfo.completeContent
        }

        private touch(evt: egret.TouchEvent) {          
            var ret = game.playerHandle.compeleteTask(this.taskId)
            var child = this.stage.getChildByName('MapView')
            child['adventureTimer'].start()
            this.parent.removeChild(this)        
        }
    }
    
    //任务面板3
    class MapTaskPopLayer3 extends eui.Component {
        confirmBtn: eui.Button
        cancelBtn: eui.Button
        contentLabel: eui.Label
        taskId: number
        taskInfo: any
        public constructor(taskInfo) {
            super()
            this.taskInfo = taskInfo
            this.taskId = taskInfo.taskId
            this.skinName = "resource/control/MapPopSkin.exml"
            this.name = "MapPopLayer"
            this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this)
            this.cancelBtn.visible = false
            var taskConfig = totalConfig[taskInfo.taskId]
            var items = taskConfig.latticeInfo.item
            var text = ''
            for(var i in items){
                var itemId = items[i][0]
                var num = items[i][1]
                text += templateName[itemId] + 'X' + num + '\n'              
            }
            this.contentLabel.text = taskConfig.text
        }

        private touch(evt: egret.TouchEvent) {
            var items = this.taskInfo.latticeInfo.item
            if(items) {
                var ret = game.playerHandle.packageIfEnough(items)
                if(ret === -1) {
                    this.parent.removeChild(this)
                    return -1
                }
                game.playerHandle.addItemsByArray(items)
            }
            if(!this.taskInfo.latticeInfo.completeContent){
                var child = this.stage.getChildByName('MapView')
                child['adventureTimer'].start()
                this.parent.removeChild(this)
                return
            }
            var mapEventPopLayer = new MapTaskPopLayer2(this.taskInfo)
            this.parent.addChild(mapEventPopLayer)  
            this.parent.removeChild(this)
        }
    }
    
    class MapPopLayer extends eui.Component {
        confirmBtn: eui.Button
        cancelBtn: eui.Button
        contentLabel: eui.Label
        latticeConfig: any
        public constructor(latticeConfig) {
            super()
            this.latticeConfig = latticeConfig
            this.skinName = "resource/control/MapPopSkin.exml"
            this.name = "MapPopLayer"
            this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this)
            this.cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.contentLabel.text = '事件内容'
        }

        private touch(evt: egret.TouchEvent) {
            if(this.latticeConfig.latticeType === 8) {
                var monsterId = this.latticeConfig.monsterId
                var battleView = new view.BattleView2(monsterId)
                this.parent.addChild(battleView)
                this.parent.removeChild(this)
            }
        }

        private close(evt: egret.TouchEvent) {
            var child = this.stage.getChildByName('MapView')
            child['adventureTimer'].start()
            this.parent.removeChild(this)
        }
    }

    class MapEventPopLayer extends eui.Component {
        contentLabel: eui.Label
        eventConfig: any
        btnGroup: eui.Group
        eventCon: string
        public constructor(eventId) {
            super()
            this.eventConfig = totalConfig[eventId]
            this.skinName = "resource/control/MapEventPopSkin.exml"
            this.name = "MapEventPopLayer"
            this.contentLabel.text = this.eventConfig.content
            this.showCon()
        }

        showCon() {
            for(var i in this.eventConfig.option) {
                var btn = new eui.Button()
                btn.skinName = "resource/control/Button2Skin.exml"
                var option = this.eventConfig.option[i]
                btn.label = option.name
                this.btnGroup.addChild(btn)
                btn['index'] = i

                btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.triggerEvent,this)
            }
        }

        showEventCon() {
            this.btnGroup.removeChildren()
            var btn = new eui.Button()
            btn.skinName = "resource/control/Button2Skin.exml"
            btn.label = '确定'
            this.btnGroup.addChild(btn)
            this.contentLabel.text = this.eventCon
            btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
        }

        private triggerEvent(evt: egret.TouchEvent) {
            var target = evt.target
            var index = target.index
            var option = this.eventConfig.option[index]
            if(option.itemId) {
                var ret = game.utilsHandle.removeItemsByBag(gplayer,option.itemId)
                if(ret === -1) {
                    this.close()
                    return
                } else {
                    this.adventure(option)
                }
            } else if(option.gold) {
                if(gplayer.basicInfo.gold < option.gold) {
                    this.close()
                    return
                } else {
                    this.adventure(option)
                }
            } else {
                this.adventure(option)

            }

            this.showEventCon()
        }

        private close() {
            var child = this.stage.getChildByName('MapView')
            child['adventureTimer'].start()
            this.parent.removeChild(this)
        }

        private adventure(option) {
            var results = option.result
            if(!results) {
                this.close()
                return
            }
            var result = game.utilsHandle.getRandomOne(results)
            console.log(result)
            //1.得到物品2.得到金钱4.损失生命5.buff加成6.损失精神7.什么都没有发生
            switch(result.eventType) {
                case 1:
                    var propId = game.utilsHandle.getRandomOne(result.getProp)
                    var item = game.utilsHandle.createItem({
                        id: propId,
                        stack: 1
                    })
                    var ret = gplayer.bag.addItem(item)
                    if(ret === -1) {
                        this.eventCon = '背包空间不足'
                        return -1
                    }
                    this.eventCon = result.content.replace('&t',templateName[propId])
                    this.eventCon = this.eventCon.replace('&v','1')
                    return 0
                    break
                case 2:
                    var value = result.value
                    gplayer.basicInfo.gold += value
                    if(gplayer.basicInfo.gold < 0) {
                        value -= gplayer.basicInfo.gold
                        gplayer.basicInfo.gold = 0
                    }
                    this.eventCon = result.content.replace('&v',value)
                    return 0
                case 4:
                    var value = result.value
                    gplayer.fightAttri.hp += value
                    if(gplayer.fightAttri.hp < 0) {
                        value -= gplayer.fightAttri.hp
                        gplayer.fightAttri.hp = 0
                        this.eventCon = '你阵亡了'
                        return -1
                    }
                    this.eventCon = result.content.replace('&v',value)
                    return 0
                case 6:
                    var value = result.value
                    gplayer.basicInfo.energy += value
                    if(gplayer.basicInfo.energy < 0) {
                        value -= gplayer.basicInfo.energy
                        gplayer.basicInfo.energy = 0
                        this.eventCon = '你阵亡了'
                        return -1
                    }
                    this.eventCon = result.content.replace('&v',value)
                    return 0
                case 5:
                    this.eventCon = result.content
                    return 0
                case 7:
                    this.eventCon = result.content
                    return 0
                    break
            }
        }
    }
}
